<template>
  <div id="businessManagement">
    <filter-box @query="getCompanyList" @changeKeyWord="changeKeyWord"></filter-box>
    <table-box :businessData="businessData" @reloadPage="getCompanyList" class="table"></table-box>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import filterBox from './filterBox'
import tableBox from './tableBox'
import { jsUtils } from 'util/util.js'
import { getCompanyList } from 'api/cooperationPlatform'
export default {
  components: {
    filterBox, tableBox
  },
  data () {
    return {
      businessData: [],
      total: 0,
      currentPage: 1,
      query: {
        pageSize: 10,
        curPage: 0,
        name: '',
        id: ''
      }
    }
  },
  created () {
    this.$emit('setTitleMsg', ['合作平台', '合作商家管理']);
    this.getCompanyList();
  },
  methods: {
    async getCompanyList () {
      if (this.query.id !== '') {
        let reg = /^[0-9]*$/;
        if (!reg.test(this.query.id)) {
          this.$alert('请输入正确的合作公司ID', '提示');
          return false;
        }
      }
      try {
        let back = await getCompanyList(this.query);
        back = back.data;
        if (back.resultCode === '200') {
          this.businessData = [...back.result];
          this.total = back.size;
          this.businessData.forEach(e => {
            e.cts = jsUtils.getSmpFormatDate(e.cts, true);
            if (e.upts != null) {
              e.upts = jsUtils.getSmpFormatDate(e.upts, true);
            }
            e.companyTypeTxt = e.companyType === 1 ? '集团内部公司' : '合作友商'
          });
        } else {
          this.$alert(back.msg);
        }
      } catch (err) {
        this.$alert('请求出错', '提示');
      }
    },
    changeKeyWord (keywordType, keyword) {
      this.query.id = '';
      this.query.name = '';
      this.query[keywordType] = keyword;
    },
    handleSizeChange (val) {
      this.query.pageSize = val;
      this.getCompanyList();
    },
    handleCurrentChange (val) {
      this.query.curPage = val - 1;
      this.getCompanyList();
    }
  }
}
</script>
<style lang="less" scoped>
  #businessManagement{
    .block{
      float: right;
      margin-top: 2rem;
    }
  }
</style>

